package com.wkbb.basic.controller.open;

import com.github.pagehelper.PageInfo;
import com.wkbb.basic.service.StudentService;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.order.PayAuditDto;
import com.wkbb.common.dto.order.PayUserCommodityInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dengjingyuan
 * @date 2019-07-29 10:49
 */
@Api(description = "订单相关接口")
@RestController
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class OrderController {

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "通过学校、年级、班级、模糊搜索字段查询学生")
    @RequestMapping(value = "/getStudentUserIdByParam", method = RequestMethod.GET)
    public ResultDTO<List<Long>> getStudentUserIdByParam(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Long gradeId,
            @ApiParam(value = "班级ID", required = false) @RequestParam(name = "classId", required = false) Long classId,
            @ApiParam(value = "关键字（搜索本页姓名,手机号,学号）", required = false) @RequestParam(name = "keyWord", required = false) String keyWord
    ){
        log.info("***********OrderController.getStudentUserIdByParam******apiVersion:{},orgId:{},gradeId:{},classId:{},keyWord:{}", apiVersion, orgId,gradeId,classId,keyWord);
        try {
            if (null == orgId) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return new ResultDTO<>(studentService.getStudentUserIdByParam(orgId,gradeId,classId,keyWord));
        } catch (Exception e) {
            log.error("***********OrderController.getStudentUserIdByParam******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }


    @ApiOperation(value = "通过未开通用户集合查询选择用户列表")
    @RequestMapping(value = "/getUserPackageByParam", method = RequestMethod.POST)
    public ResultDTO<PageInfo<PayUserCommodityInfoDto>> getUserPackageByParam(
            @ApiParam(value = "开通审核对象", required = true) @RequestBody(required = true) PayAuditDto payAuditDto
    ){
        if(CollectionUtils.isEmpty(payAuditDto.getUserIds())){
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }
        return new ResultDTO<>( studentService.getUserPackageByParam(payAuditDto.getUserIds(),payAuditDto.getPageNo(),payAuditDto.getPageSize()));
    }
}
